package com.xiaoyu.lanling.feature.user.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import android.text.TextUtils
import com.xiaoyu.base.event.NoReceiverJsonEvent
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.user.*

object UserInfoData {

    const val MAX_NAME_LENGTH = 10
    const val MAX_SOLILOQUY_LENGTH = 36

    fun profile(requestTag: Any, uid: String) {
        RequestCreator.create<UserProfileEvent>(requestTag, UserProfileEvent::class.java)
                .setRequestUrl(HttpConfig.USER_PROFILE)
                .addQueryData("uid", uid)
                .enqueue()
    }

    fun setName(requestTag: Any, name: String) {
        getProfileCreator(requestTag)
                .addPostData("name", name)
                .enqueue()
    }

    fun setSoliloquy(requestTag: Any, soliloquy: String) {
        getProfileCreator(requestTag)
                .addPostData("soliloquy", soliloquy)
                .enqueue()
    }

    fun setAvatar(requestTag: Any, sex: String) {
        getProfileCreator(requestTag)
                .addPostData("avatar", sex)
                .enqueue()
    }

    fun setBirthday(requestTag: Any, sex: String) {
        getProfileCreator(requestTag)
                .addPostData("birthday", sex)
                .enqueue()
    }

    fun setProfile(requestTag: Any, key: String, value: String) {
        getProfileCreator(requestTag)
                .addPostData(key, value)
                .enqueue()
    }

    fun getProfileCreator(requestTag: Any): RequestCreator<UserSetProfileEvent> {
        return RequestCreator.create<UserSetProfileEvent>(requestTag, UserSetProfileEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_SET_PROFILE)
                .setPostEventWhenFail(true)
    }

    fun setProfile(requestTag: Any, name: String?, avatar: String?, sex: String?, birthday: String?, province: String?, city: String?, county: String?, inviteCode: String?) {
        getProfileCreator(requestTag)
                .setPostEventWhenFail(true)
                .addPostData("name", name)
                .addPostData("avatar", avatar)
                .addPostData("sex", sex)
                .addPostData("birthday", birthday)
                .addPostData("province", province)
                .addPostData("city", city)
                .addPostData("county", county)
                .addPostData("inviteCode", inviteCode)
                .enqueue()
    }

    fun homePage(requestTag: Any, uid: String) {
        RequestCreator.create<UserHomePageEvent>(requestTag, UserHomePageEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_HOME_PAGE)
                .addQueryData("uid", uid)
                .enqueue()
    }

    fun setLocation(requestTag: Any, province: String?, city: String?, county: String?) {
        getProfileCreator(requestTag)
                .addPostData("province", province)
                .addPostData("city", city)
                .addPostData("county", county)
                .enqueue()
    }

    fun subscribeUser(requestTag: Any, uid: String, subscribe: Boolean) {
        RequestCreator.create<SubscribeUserEvent>(requestTag, SubscribeUserEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_SUBSCRIBE_USER)
                .addQueryData("uid", uid)
                .addQueryData("subscribe", subscribe)
                .enqueue()
    }

    fun blackUser(requestTag: Any, uid: String, black: Boolean) {
        RequestCreator.create<BlackUserEvent>(requestTag, BlackUserEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_BLACK_USER)
                .addQueryData("uid", uid)
                .addQueryData("black", black)
                .enqueue()
    }

    fun setPhotos(requestTag: Any, photos: List<String>) {
        RequestCreator.create<UserSetPhotoEvent>(UserSetPhotoEvent::class.java)
                .setRequestUrl(HttpConfig.USER_SET_PHOTOS)
                .addQueryData("photos", TextUtils.join(",", photos))
                .setRequestHandler(object : RequestDefaultHandler<UserSetPhotoEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): UserSetPhotoEvent {
                        return UserSetPhotoEvent(requestTag, photos)
                    }

                    override fun onRequestFinish(event: UserSetPhotoEvent) {
                        event.post()
                    }
                })
                .enqueue()
    }

    fun getVoice(requestTag: Any, uid: String) {
        RequestCreator.create<UserVoiceEvent>(requestTag, UserVoiceEvent::class.java)
                .setRequestUrl(HttpConfig.USER_GET_VOICE)
                .addQueryData("uid", uid)
                .enqueue()
    }

    fun getVoiceTemplateText(requestTag: Any) {
        RequestCreator.create<UserVoiceTemplateTextEvent>(requestTag, UserVoiceTemplateTextEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_VOICE_TEMPLATE_TEXT)
                .enqueue()
    }

    fun uploadVoice(requestTag: Any, url: String, duration: Int) {
        RequestCreator.create<UserUploadVoiceEvent>(requestTag, UserUploadVoiceEvent::class.java)
                .setRequestUrl(HttpConfig.USER_UPLOAD_VOICE)
                .setPostEventWhenFail(true)
                .addQueryData("voiceUrl", url)
                .addQueryData("voiceDuration", duration)
                .enqueue()
    }

    fun mine(requestTag: Any) {
        RequestCreator.create<UserMineEvent>(requestTag, UserMineEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_MY_TAB)
                .enqueue()
    }

    fun setUserSetting(requestTag: Any, cupidAndAccostSwitch: Boolean) {
        RequestCreator.create<UserSettingEvent>(requestTag, UserSettingEvent::class.java)
                .setRequestUrl(HttpConfig.USER_SET_USER_SETTING)
                .addQueryData("cupidAndAccostSwitch", cupidAndAccostSwitch)
                .enqueue()
    }
}